package com.wanmait.bootdemo.controller;

import com.wanmait.bootdemo.annotation.IgnoreToken;
import com.wanmait.bootdemo.pojo.Admin;
import com.wanmait.bootdemo.service.AdminService;
import com.wanmait.bootdemo.util.CookieUtils;
import com.wanmait.bootdemo.util.JWTUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/manage")
public class CommonController {

    @Resource
    private AdminService adminService;
            @RequestMapping("login")
            @ResponseBody
            @IgnoreToken
            public String login(@RequestBody Admin admin, HttpServletRequest request, HttpServletResponse response){
                Admin loginAdmin = adminService.login(admin);
                if(loginAdmin!=null){
                    String token = JWTUtils.createToken(loginAdmin);
                    CookieUtils.setCookie(request,response,"token",token);
;                      return token;
        }
        return "登录失败";
    }
}
